<?php
/**
 * Slides functions and definitions
 *
 */

if ( !function_exists( 'slide_posttype_init' ) ) {
	function slide_posttype_init() {

		$slide_labels = array(
			'name' => _x('Slides', 'post type general name'),
			'singular_name' => _x('Slide', 'post type singular name'),
			'add_new' => _x('Add New', 'slide'),
			'add_new_item' => __('Add New Slide'),
			'edit_item' => __('Edit Slide'),
			'new_item' => __('New Slide'),
			'all_items' => __('All Slides'),
			'view_item' => __('View Slide'),
			'search_items' => __('Search Slides'),
			'not_found' =>  __('No slides found'),
			'not_found_in_trash' => __('No slides found in Trash'), 
			'parent_item_colon' => '',
			'menu_name' => __('Slides')

		);
		$slide_args = array(
			'labels' => $slide_labels,
			'public' => false,
			'publicly_queryable' => false,
			'show_ui' => true, 
			'show_in_menu' => true, 
			'query_var' => false,
			'rewrite' => false,
			'capability_type' => 'post',
			'has_archive' => false, 
			'hierarchical' => false,
			'menu_position' => '5.3',
			'menu_icon' => CINGLE_URI . 'img/posttype-slide.png',
			'supports' => array( 'title' )
		); 
		register_post_type( 'slide', $slide_args );

	}
	add_action( 'init', 'slide_posttype_init' );
}

// Add metabox
function slide_metabox(){
	add_meta_box('slide-metabox', 'Slide Data', 'slide_metabox_callback', 'slide', 'normal');
	add_meta_box('slide-metabox-display-info', 'Slide Display Info', 'slide_metabox_display_callback', 'slide', 'side');
}
add_action('admin_init', 'slide_metabox');

// Slide metabox callback
function slide_metabox_callback($post){
	$key = 0;
	$create = true;
	$slides = unserialize( get_post_meta($post->ID,'_slide_options', true) );
	echo '<div class="metabox-tabs-div">';
	echo '<ul class="metabox-tabs">';
	if( !empty($slides) ){
		foreach ($slides as $key => $value) {
			$class = $key == 1 ? 'active' : '';
			echo '<li class="tab-menu '.$class.'" rel="#tab-'.$key.'">'.__('Slide '.$key,'dw-simplex').'</li>';
		}
	}else{
		echo '<li class="tab-menu active" rel="#tab-1">'.__('Slide 1', 'dw-simplex').'</li>';
	}
	echo '<li id="tab-create" rel="#tab-create">'.__('+','dw-simplex').'</li>';
	echo '</ul>';
	if( !empty( $slides ) ){
		foreach ($slides as $key => $slide) {
			slide_metabox_callback_html($key, $slide, false);
		}
	}else{
		slide_metabox_callback_html(1, array(), false);
	}
	slide_metabox_callback_html(0);
	echo '</div>';
}

function slide_metabox_display_callback(){
	global $post;
	echo '<p><strong>Shortcode</strong>:<br />';
	echo '<code>[dw_simplex_slide slug="'.$post->post_name.'"]</code>';
	echo '<p><strong>PHP Function</strong>:<br />';
	echo '<code>dw_simplex_display_slide( \''.$post->post_name.'\' );</code>';
}

function slide_metabox_callback_html($key, $option = array() ){
	$option = wp_parse_args($option, array( 
			'title'	=>	'',
			'image'	=>	'',
			'url'	=>	'',
			'background'	=>	''
		) );
	$class = $key == 0 ? 'tab-create ' : 'tab-content ';
	$class .= 'tab-'.$key.' ';
	$class .= $key == 1 ? 'active ' : '';

	?>
	<div id="tab-<?php echo $key; ?>" class="<?php echo $class ?>">
		<table width="100%">	
			<tr align="left">
				<th class="left"><?php _e('Slide Title','dw-simplex');  ?></th>
				<td>
					<p><input type="text" name="slide[<?php echo $key; ?>][title]" id="slide-<?php echo $key; ?>-title" class="regular-text" value="<?php echo $option['title']; ?>" ></p>
				</td>
			</tr>
			<tr align="left" class="alternate">
				<th class="left"><?php _e('Slide Images','dw-simplex') ?></th>
				<td>
					<p>
						<input type="text" id="slide-<?php echo $key; ?>-image" name="slide[<?php echo $key; ?>][image]" class="regular-text" value="<?php echo $option['image']; ?>" />
						<button onclick="return false" class="simplex_upload_btn button" rel="#slide-<?php echo $key; ?>-image" ><?php _e('Upload image', 'dw-simplex'); ?></button>
					</p>
				</td>
			</tr>
			<tr align="left">
				<th class="left"><?php _e('Slide URL','dw-simplex');  ?></th>
				<td>
					<p><input type="text" name="slide[<?php echo $key; ?>][url]" id="slide-<?php echo $key; ?>-url" class="regular-text" value="<?php echo $option['url']; ?>" ></p>
				</td>
			</tr>	
			<tr align="left" class="alternate">
				<th class="left"><?php _e('Slide Background','dw-simplex') ?></th>
				<td>
					<p>
						<input type="text" id="slide-<?php echo $key; ?>-background" name="slide[<?php echo $key; ?>][background]" class="regular-text" value="<?php echo $option['background']; ?>" />
						<button onclick="return false" class="simplex_upload_btn button" rel="#slide-<?php echo $key; ?>-background" ><?php _e('Upload image', 'dw-simplex'); ?></button>
					</p>
				</td>
			</tr>
			<tr>
				<th class="left" ></th>
				<td>
					<input name="delete_slide" id="delete_slide" type="submit" value="<?php _e('Remove','dw-simplex') ?>" class="button remove-slide" >
				</td>
			</tr>
		</table>
	</div> <!-- End tab <?php echo $key; ?> -->
	<?php
}


// Action when save slide
add_action('save_post', 'dw_simplex_admin_save_slide');
function dw_simplex_admin_save_slide($post_id){
	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
		return;
	if( isset($_POST['post_type']) && 'slide' == $_POST['post_type'] ){
		if( isset($_POST['slide']) ){
			unset($_POST['slide'][0]);
			$slides = dw_simplex_rearrange_slides($_POST['slide']);
			update_post_meta($post_id, '_slide_options', serialize($slides) );
		}
	}
}
// Action when remove slide
add_action('wp_ajax_dw_simplex_remove_slide_posttype_slide', 'dw_simplex_remove_slide_posttype_slide');
function dw_simplex_remove_slide_posttype_slide(){
	$result = new stdClass();
	$result->status = 'error';
	if( isset($_POST['slide']) && isset($_POST['number']) ){
		$slides = get_post_meta($_POST['slide'], '_slide_options', true);
		if( !empty($slides) ){
			$slides = unserialize($slides);
			unset($slides[ $_POST['number'] ]);
			$slides = serialize(dw_simplex_rearrange_slides($slides));
			update_post_meta($_POST['slide'], '_slide_options', $slides);
			$result->status = 'success';
		}
	}
	echo json_encode($result);
	exit(0);
}

// Rearrange slides list
if(!function_exists('dw_simplex_rearrange_slides') ){
	function dw_simplex_rearrange_slides($slides){
		$rearr = array(); $i = 0;
		foreach ($slides as $slide ) {
			$i++;
			$rearr[$i] = $slide;
		}
		return $rearr;
	}
}

// show button send for thickbox media upload
function dw_simplex_admin_thichbox($args){
	if( get_post_type($_GET['post_id']) == 'slide' ){
		$args['send'] = true;
	}

	return $args;
}
add_filter('get_media_item_args','dw_simplex_admin_thichbox');

function dw_simplex_get_slide( $id = false ){
	if( false === $id ){
		global $post;
		$id = $post->ID;
	}
	$slides = unserialize(get_post_meta($id, '_slide_options', true) );
	return $slides;
}

function dw_simplex_display_slide( $slug = false){
	global $post;
	if( $slug ){
		$args=array(
		  'name' => $slug,
		  'post_type' => 'slide',
		  'post_status' => 'publish',
		  'numberposts' => 1
		);
		$slide = get_posts($args);
		$id = !empty($post) ? $slide[0]->ID : $post->ID;
	}else{
		$id = $post->ID;
	}
	$slides = dw_simplex_get_slide($id);
	if( !empty($slides) ){
?>
<div id="slide-<?php echo $id  ?>" class="carousel slide dw-simplex-slide">
	<div class="carousel-inner">
		<?php 
			foreach ($slides as $key => $slide) {
		?>
		<div class="item <?php echo $key == 1 ? 'active' : '' ?>"<?php if($slide['background']) echo ' style="background: url('.$slide['background'].');"'; ?>>
			<?php if($slide['image']) : ?>
			<img src="<?php echo $slide['image']; ?>" alt="<?php _e( $slide['title'], 'dw-simplex' ); ?>">
			<?php endif; ?>
			<?php if($slide['title']) : ?>
			<div class="carousel-caption">
				<h4><?php _e( $slide['title'], 'dw-simplex' ); ?></h4>
			</div>
			<?php endif; ?>
		</div>
		<?php
			}
		?>
	</div>
	<a class="left carousel-control" href="#slide-<?php echo $id  ?>" data-slide="prev">‹</a>
	<a class="right carousel-control" href="#slide-<?php echo $id  ?>" data-slide="next">›</a>
</div>
<?php
	}
}

// Short code
function dw_simplex_slide_shortcode($atts){
	extract(shortcode_atts(array(
		"slug" => false
	), $atts));
	dw_simplex_display_slide($slug);
}
add_shortcode('dw_simplex_slide', 'dw_simplex_slide_shortcode');

add_action('manage_posts_custom_column',  'dw_simplex_slide_show_columns');
function dw_simplex_slide_show_columns($name) {
	global $post;
	switch ($name) {
		case 'slide_shortcode':
			echo '<code>[dw_simplex_slide slug="'.$post->post_name.'"]</code>';
			break;
		case 'slide_function':
			echo '<code>dw_simplex_display_slide( \''.$post->post_name.'\' );</code>';
			break;
	}
}